package utils

import (
	"fmt"
	"sync"
	"time"
)

func worker(id int, jobs <-chan int, results chan<- int) {
	for j := range jobs {
		fmt.Println("worker", id, "started job", j)
		time.Sleep(time.Second)
		fmt.Println("worker", id, "finished job", j)
		results <- j * 2
	}
}

func WorkerP() {
	const numJobs = 5
	jobs := make(chan int, numJobs)
	results := make(chan int, numJobs)

	for w := 1; w <= 3; w++ {
		go worker(w, jobs, results)
	}

	for j := 1; j <= numJobs; j++ {
		jobs <- j
	}
	close(jobs)

	for a := 1; a <= numJobs; a++ {
		<-results
	}
}

func worker2(id int) {
	fmt.Printf("Workder %d starting \n", id)
	time.Sleep(time.Second)
	fmt.Printf("Worker %d done\n", id)
}

func WgP() {
	var wg sync.WaitGroup
	wg2 := &sync.WaitGroup{}

	for i := 1; i <= 5; i++ {
		wg.Add(1)
		i := i

		go func ()  {
			defer wg.Done()
			worker2(i)
		}()
	}

	wg.Wait()

	for i := 1; i <= 10; i++ {
		wg2.Add(1)
		i := i

		go func ()  {
			defer wg2.Done()
			worker2(i)
		} ()
	}

	wg2.Wait()
}